package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.po.whitelist.WhiteListPo;
import com.bwda.dsrs.filecenter.domain.condition.whitelist.WhiteListCondition;
import com.bwda.dsrs.filecenter.domain.form.whitelist.WhiteListQueryForm;
import com.bwda.dsrs.filecenter.domain.form.whitelist.WhiteListUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.whitelist.WhiteListCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.whitelist.WhiteListVo;
import com.bwda.dsrs.filecenter.service.WhiteListService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;

import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 白名单
 * @author yangjun
 */
@RestController
@RequestMapping("/b/whitelist")
@Api(value = "/b/whitelist", tags = "白名单")
public class WhiteListController extends BaseController {

	@Autowired
	private WhiteListService whiteListService;

	@ApiOperation(value = "查询白名单",notes = "查询白名单",httpMethod = "GET")
	@Loggable(descp = "查询白名单",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<WhiteListVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		WhiteListPo po = whiteListService.queryWithValid(id);
		WhiteListVo vo = CopyUtil.transfer(po, WhiteListVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询白名单数量",notes = "查询白名单数量",httpMethod = "GET")
	@Loggable(descp = "查询白名单数量",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid WhiteListQueryForm form) throws BusinessServiceException {
		WhiteListCondition condition = this.getConditionByQueryForm(form);
		int count = whiteListService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询白名单列表",notes = "查询白名单列表",httpMethod = "GET")
	@Loggable(descp = "查询白名单列表",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<WhiteListVo>> queryList(@ModelAttribute@Valid WhiteListQueryForm form) throws BusinessServiceException {
		WhiteListCondition condition = this.getConditionByQueryForm(form);
		List<WhiteListPo> poList = whiteListService.queryList(condition);
		List<WhiteListVo> voList = CopyUtil.transfer(poList, WhiteListVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询白名单列表(带分页)",notes = "查询白名单列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询白名单列表(带分页)",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<WhiteListVo>> queryPageList(@ModelAttribute@Valid WhiteListQueryForm form) throws BusinessServiceException {
		WhiteListCondition condition = this.getConditionByQueryForm(form);
		List<WhiteListVo> voList = new ArrayList<>();
		int count = whiteListService.queryCount(condition);
		if (count > 0) {
			List<WhiteListPo> poList = whiteListService.queryList(condition);
			voList = CopyUtil.transfer(poList, WhiteListVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增白名单",notes = "新增白名单",httpMethod = "POST")
	@Loggable(descp = "新增白名单",type = LogTypeEnum.CREATE,include = "")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<WhiteListVo> add(@ModelAttribute@Valid WhiteListCreateForm form) throws BusinessServiceException {
		WhiteListPo po = CopyUtil.transfer(form, WhiteListPo.class);
		po.setId(1);
		whiteListService.insert(po);
		WhiteListVo vo = CopyUtil.transfer(po, WhiteListVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改白名单",notes = "修改白名单",httpMethod = "POST")
	@Loggable(descp = "修改白名单",type = LogTypeEnum.UPDATE,include = "")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid WhiteListUpdateForm form) throws BusinessServiceException {
		WhiteListPo po = CopyUtil.transfer(form, WhiteListPo.class);
		whiteListService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除白名单",notes = "删除白名单",httpMethod = "POST")
	@Loggable(descp = "删除白名单",type = LogTypeEnum.DELETE,include = "")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		whiteListService.delete(id);
		return getSuccessResult();
	}

	/**
	 * WhiteListQueryForm转换为WhiteListCondition
	 *
	 * @param form
	 * @return
	 */
	private WhiteListCondition getConditionByQueryForm(WhiteListQueryForm form) {
		WhiteListCondition condition = CopyUtil.transfer(form, WhiteListCondition.class);
		return condition;
	}

}